package com.liujg.future.api;

import com.liujg.future.model.dto.*;
import com.liujg.future.model.entity.*;
import com.liujg.future.model.resp.*;
import com.liujg.future.service.ShopFrontService;
import com.liujg.future.util.JwtTokenUtils;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.net.http.HttpRequest;
import java.util.List;

@RestController
@Slf4j
public class RabbitShopApi {

    @Autowired
    private ShopFrontService shopFrontService;


    @GetMapping("/home/banner")
    public BaseResult<List<Banner>> homeBanner() {
        return new BaseResult<>(shopFrontService.banners());
    }

    @GetMapping("/home/category/mutli")
    public BaseResult<List<Category>> homeCategory() {
        return new BaseResult<>(shopFrontService.homeCategory());


    }

    @GetMapping("/home/hot/mutli")
    public BaseResult<List<Hot>> homeHot() {
        return new BaseResult<>(shopFrontService.homeHot());

    }

    @GetMapping("/home/goods/guessLike")
    public BaseResult<PageVO<GuessLike>> guessLike(HttpServletRequest request,PageDTO pageDTO) {
        return new BaseResult<>(shopFrontService.guessLike(pageDTO));

    }

    /**
     * 热门推荐详情
     *
     */
    @GetMapping("/hot/preference")
    public BaseResult<HotDetail> hotPreference(@RequestParam(value = "subType",required = false)String subType,
                                                       @RequestParam(value = "page",required = false,defaultValue = "1") Integer page,
                                                       @RequestParam(value = "pageSize",required = false,defaultValue = "10")Integer pageSize) {
        return new BaseResult<>(shopFrontService.hotPreference("/hot/preference",subType,page,pageSize));
    }

    @GetMapping("/hot/inVogue")
    public BaseResult<HotDetail> hotInVogue(@RequestParam(value = "subType",required = false)String subType,
                                                       @RequestParam(value = "page",required = false,defaultValue = "1") Integer page,
                                                       @RequestParam(value = "pageSize",required = false,defaultValue = "10")Integer pageSize) {
        return new BaseResult<>(shopFrontService.hotPreference("/hot/inVogue",subType,page,pageSize));
    }
    @GetMapping("/hot/oneStop")
    public BaseResult<HotDetail> hotOneStop(@RequestParam(value = "subType",required = false)String subType,
                                                       @RequestParam(value = "page",required = false,defaultValue = "1") Integer page,
                                                       @RequestParam(value = "pageSize",required = false,defaultValue = "10")Integer pageSize) {
        return new BaseResult<>(shopFrontService.hotPreference("/hot/oneStop",subType,page,pageSize));
    }
    @GetMapping("/hot/new")
    public BaseResult<HotDetail> hotNew(@RequestParam(value = "subType",required = false)String subType,
                                                       @RequestParam(value = "page",required = false,defaultValue = "1") Integer page,
                                                       @RequestParam(value = "pageSize",required = false,defaultValue = "10")Integer pageSize) {
        return new BaseResult<>(shopFrontService.hotPreference("/hot/new",subType,page,pageSize));
    }

    @GetMapping("/category/top")
    public BaseResult<List<CategoryTop>> categoryTop() {
        return new BaseResult<>(shopFrontService.categoryTop());

    }

    @GetMapping("/goods")
    public BaseResult<GoodsDetail> goodsDetail(@RequestParam("id") String id) {
        return new BaseResult<>(shopFrontService.goodsDetail(id));

    }


    // 小程序登录
    @PostMapping("/login/wxMin/simple")
    public BaseResult<LoginVO> wxMinSimpleLogin(@RequestBody LoginDTO loginDTO) {
        return new BaseResult<>(shopFrontService.wxMinSimpleLogin(loginDTO));
    }



    /**
     * 获取个人信息
     * @param request request
     * @return BaseResult<UserInfo>
     */
    @GetMapping("/member/profile")
    public BaseResult<UserInfo> memberProfile(HttpServletRequest request) {
        String tokenId = request.getHeader("Authorization");
        String userId = JwtTokenUtils.getMsg(tokenId).getClaim("userId").asString();
        // 过期时间
        // String expire = JwtTokenUtils.getMsg(tokenId).getClaim("expire").asString();
        return new BaseResult<>(shopFrontService.memberProfile(userId));
    }


    @PutMapping("/member/profile")
    public BaseResult<UserInfo> memberProfileModify(HttpServletRequest request,@RequestBody UserInfoDTO dto) {
        String tokenId = request.getHeader("Authorization");
        String userId = JwtTokenUtils.getMsg(tokenId).getClaim("userId").asString();
        return new BaseResult<>(shopFrontService.memberProfileModify(userId,dto));
    }

    @PostMapping("/member/profile/avatar")
    public BaseResult<String> memberProfileAvatarModify(HttpServletRequest request,@RequestParam("file") MultipartFile file) {
        String tokenId = request.getHeader("Authorization");
        String userId = JwtTokenUtils.getMsg(tokenId).getClaim("userId").asString();
        return new BaseResult<>(shopFrontService.memberProfileAvatarModify(userId,file));
    }


    @PostMapping("/member/address")
    public BaseResult<Integer> memberAddressAdd(HttpServletRequest request,@RequestBody AddressDTO dto) {
        String tokenId = request.getHeader("Authorization");
        String userId = JwtTokenUtils.getMsg(tokenId).getClaim("userId").asString();
        return new BaseResult<>(shopFrontService.memberAddressAdd(userId,dto));
    }

    @DeleteMapping("/member/address/{id}")
    public BaseResult<String> memberAddressDelete(HttpServletRequest request,@PathVariable("id") String id) {
        String tokenId = request.getHeader("Authorization");
        String userId = JwtTokenUtils.getMsg(tokenId).getClaim("userId").asString();
        return new BaseResult<>(shopFrontService.memberAddressDelete(userId,id));
    }

    @PutMapping("/member/address/{id}")
    public BaseResult<Integer> memberAddressModify(HttpServletRequest request,@PathVariable("id") String id,@RequestBody AddressDTO dto) {
        String tokenId = request.getHeader("Authorization");
        String userId = JwtTokenUtils.getMsg(tokenId).getClaim("userId").asString();
        return new BaseResult<>(shopFrontService.memberAddressModify(userId,id,dto));
    }

    @GetMapping("/member/address/{id}")
    public BaseResult<Address> memberAddressDetail(HttpServletRequest request, @PathVariable("id") String id) {
        String tokenId = request.getHeader("Authorization");
        String userId = JwtTokenUtils.getMsg(tokenId).getClaim("userId").asString();
        return new BaseResult<>(shopFrontService.memberAddressDetail(userId,id));

    }

    @GetMapping("/member/address")
    public BaseResult<List<Address>> memberAddressList(HttpServletRequest request) {
        String tokenId = request.getHeader("Authorization");
        String userId = JwtTokenUtils.getMsg(tokenId).getClaim("userId").asString();
        return new BaseResult<>(shopFrontService.memberAddressList(userId));
    }


    // ------------ 购物车 -----------------  //
    @PostMapping("/member/cart")
    public BaseResult<Cart> cartAdd(HttpServletRequest request,@RequestBody CartAddDTO dto) {
        String tokenId = request.getHeader("Authorization");
        String userId = JwtTokenUtils.getMsg(tokenId).getClaim("userId").asString();

        return new BaseResult<>(shopFrontService.cartAdd(userId,dto));
    }

    /**
     * 购物车列表
     * @param request
     * @return
     */

    @GetMapping("/member/cart")
    public BaseResult<List<Cart>> cartList(HttpServletRequest request) {
        String tokenId = request.getHeader("Authorization");
        String userId = JwtTokenUtils.getMsg(tokenId).getClaim("userId").asString();

        return new BaseResult<>(shopFrontService.cartList(userId));

    }

    /**
     * 删除购物车列表
     * @param request
     * @param ids
     * @return
     */
    @DeleteMapping("/member/cart")
    public BaseResult<Integer> cartDelete(HttpServletRequest request,@RequestBody IdsDTO ids) {
        String tokenId = request.getHeader("Authorization");
        String userId = JwtTokenUtils.getMsg(tokenId).getClaim("userId").asString();

        return new BaseResult<>(shopFrontService.deleteCart(userId,ids));
    }

    @PutMapping("/member/cart/{skuId}")
    public BaseResult<Cart> cartModify(HttpServletRequest request,@PathVariable("skuId") String skuId,@RequestBody CartModifyDTO dto) {
        String tokenId = request.getHeader("Authorization");
        String userId = JwtTokenUtils.getMsg(tokenId).getClaim("userId").asString();
        dto.setSkuId(skuId);
        return new BaseResult<>(shopFrontService.modifyCart(userId,dto));
    }
    @PutMapping("/member/cart/selected")
    public BaseResult<Integer> cartSelected(HttpServletRequest request,@RequestBody CartSelectedDTO dto){
        String tokenId = request.getHeader("Authorization");
        String userId = JwtTokenUtils.getMsg(tokenId).getClaim("userId").asString();
        return new BaseResult<>(shopFrontService.cartSelected(userId,dto));
    }
    // -------------订单  --------------- //

    /**
     * （购物车页面）填写订单-获取预付订单
     */
    @GetMapping("/member/order/pre")
    public BaseResult<OrderPreVO> orderPre(HttpServletRequest request) {
        String tokenId = request.getHeader("Authorization");
        String userId = JwtTokenUtils.getMsg(tokenId).getClaim("userId").asString();
        return new BaseResult<>(shopFrontService.orderPre(userId));
    }

    /**
     * 商品页立即购买
     * @param request
     * @return
     */
    @GetMapping("/member/order/pre/now")
    public BaseResult<OrderPreVO> orderPreNow(HttpServletRequest request,@RequestParam("count")Integer count,@RequestParam("skuId")String skuId) {
        String tokenId = request.getHeader("Authorization");
        String userId = JwtTokenUtils.getMsg(tokenId).getClaim("userId").asString();
        return new BaseResult<>(shopFrontService.orderPreNow(userId,count,skuId));
    }
    /**
     * 再次购买订单
     */
    @GetMapping("/member/order/repurchase/{id}")
    public BaseResult<OrderPreVO> orderRepurchase(HttpServletRequest request,@PathVariable("id") String id) {
        String tokenId = request.getHeader("Authorization");
        String userId = JwtTokenUtils.getMsg(tokenId).getClaim("userId").asString();
        return new BaseResult<>(shopFrontService.orderRepurchase(userId,id));
    }

    /**
     * 购物车创建订单 提交订单
     * @return id 订单id
     */
    @PostMapping("/member/order")
    public BaseResult<Order> orderCreate(HttpServletRequest request,@RequestBody OrderCreateDTO dto) {
        String tokenId = request.getHeader("Authorization");
        String userId = JwtTokenUtils.getMsg(tokenId).getClaim("userId").asString();
        return new BaseResult<>(shopFrontService.orderCreate(userId,dto));
    }

    /**
     * 订单详情
     */
    @GetMapping("/member/order/{id}")
    public BaseResult<Order> orderDetail(HttpServletRequest request,@PathVariable("id") String id) {
        String tokenId = request.getHeader("Authorization");
        String userId = JwtTokenUtils.getMsg(tokenId).getClaim("userId").asString();
        return new BaseResult<>(shopFrontService.orderDetail(userId,id));
    }

    // ----------------支付--------------------//

    @GetMapping("/pay/mock")
    public BaseResult<String> payMock(HttpServletRequest request,@RequestParam("orderId") String orderId) {
        String tokenId = request.getHeader("Authorization");
        String userId = JwtTokenUtils.getMsg(tokenId).getClaim("userId").asString();
        return new BaseResult<>(shopFrontService.payMock(userId,orderId));


    }

    /**
     * 模拟发货
     * 仅在 订单状态为待发货 时，可模拟发货，调用后订单状态修改为待收货，包含模拟物流
     */

    @GetMapping("/member/order/consignment/{id}")
    public BaseResult<String> orderConsignment(HttpServletRequest request,@PathVariable("id") String id) {
        String tokenId = request.getHeader("Authorization");
        String userId = JwtTokenUtils.getMsg(tokenId).getClaim("userId").asString();
        return new BaseResult<>(shopFrontService.orderConsignment(userId,id));
    }

    /**
     * 确认收货
     * 仅在订单状态为待收货时，可确认收货。
     */
    @PutMapping("/member/order/{id}/receipt")
    public BaseResult<Order> orderReceipt(HttpServletRequest request,@PathVariable("id") String id) {

        String tokenId = request.getHeader("Authorization");
        String userId = JwtTokenUtils.getMsg(tokenId).getClaim("userId").asString();
        return new BaseResult<>(shopFrontService.orderReceipt(userId,id));
    }

    /**
     * 获取订单物流
     */
    @GetMapping("/member/order/{id}/logistics")
    public BaseResult<LogisticsVO> orderLogistics(HttpServletRequest request,@PathVariable("id") String id) {
        String tokenId = request.getHeader("Authorization");
        String userId = JwtTokenUtils.getMsg(tokenId).getClaim("userId").asString();
        return new BaseResult<>(shopFrontService.orderLogistics(userId,id));
    }

    /**
     * 删除订单
     * 仅在订单状态为待评价，已完成，已取消时，可删除订单。
     */
    @DeleteMapping("/member/order")
    public BaseResult<Integer> orderDelete(HttpServletRequest request,@RequestBody IdsDTO ids) {
        String tokenId = request.getHeader("Authorization");
        String userId = JwtTokenUtils.getMsg(tokenId).getClaim("userId").asString();
        return new BaseResult<>(shopFrontService.orderDelete(userId,ids));
    }

    /**
     * 取消订单
     * 仅在订单状态为待付款时，可取消订单。
     */
    @PutMapping("/member/order/{id}/cancel")
    public BaseResult<Order> orderCancel(HttpServletRequest request,@PathVariable("id") String id) {
        String tokenId = request.getHeader("Authorization");
        String userId = JwtTokenUtils.getMsg(tokenId).getClaim("userId").asString();
        return new BaseResult<>(shopFrontService.orderCancel(userId,id));
    }

    /**
     * 获取订单列表
     * 订单状态，1为待付款、2为待发货、3为待收货、4为待评价、5为已完成、6为已取消，未传该参数或0为全部
     */
    @GetMapping("/member/order")
    public BaseResult<PageVO<Order>> orderPage(HttpServletRequest request,
                                               @RequestParam(value = "orderState",required = false) Integer orderState,
                                               @RequestParam(value = "page",required = false,defaultValue = "1") Integer page,
                                               @RequestParam(value = "pageSize",required = false,defaultValue = "10")Integer pageSize) {
        String tokenId = request.getHeader("Authorization");
        String userId = JwtTokenUtils.getMsg(tokenId).getClaim("userId").asString();
        return new BaseResult<>(shopFrontService.orderPage(userId,orderState,page,pageSize));
    }


}
